//
//  CommoneTools.m
//  dzm
//
//  Created by dzmmac on 14-9-26.
//  Copyright (c) 2014年 dzmmac. All rights reserved.
//

#import "CommoneTools.h"
#import "AppConfig.h"
#import <UIKit/UIKit.h>
#import "NSUserDefaults+Addition.h"
#import "AppColorConfig.h"
#import "NSString+Additions.h"
#import "MainInfoTitleFields.h"

@implementation CommoneTools

+ (CommoneTools *) sharedManager{
    static CommoneTools *sharedManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedManagerInstance = [[self alloc] init];
    });
    return sharedManagerInstance;
}

+ (UIColor *)getHexColor:(NSString *)hexColor{
    if ([hexColor isEqualToString:@""]) {
        return [UIColor orangeColor];
    }
    hexColor = [hexColor stringByReplacingOccurrencesOfString:@"#" withString:@""];
    unsigned int red,green,blue;
    NSRange range;
    range.length = 2;
    range.location = 0;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
    range.location = 2;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
    range.location = 4;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
    return UI_RGBA(red, green, blue, 1);
}

//手机号验证
+(BOOL)isPhoneNumber:(NSString *)phoneNumber{
    if ([phoneNumber length]==0||phoneNumber==nil)
    {
        return NO;
    }
    NSString *Regex = @"^(13[0-9]|15[0-9]|18[0-9]|17[0-9]|147)\\d{8}$";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    return [emailTest evaluateWithObject:phoneNumber];
}

+(void)saveDishFile:(NSString *)fileName dic:(NSDictionary *)dic
{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
    [dic writeToFile:filename atomically:YES];
}

+(NSDictionary *)loadDishFile:(NSString *)fileName
{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
    NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:filename];
    return dic;
}
+(NSString *)filePathWithFileName:(NSString *)fileName
{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
    return filename;
}

+(BOOL)deleteSingleFile:(NSString *)filePath{
    NSError *err = nil;
    if (nil == filePath) {
        return NO;
    }
    NSFileManager *appFileManager = [NSFileManager defaultManager];
    if (![appFileManager fileExistsAtPath:filePath]) {
        return YES;
    }
    if (![appFileManager isDeletableFileAtPath:filePath]) {
        return NO;
    }
    return [appFileManager removeItemAtPath:filePath error:&err];
}


/**
 *  改变视图的约束
 *
 *  @param view           需要改变的视图
 *  @param constraintHeight 约束的尺寸
 */
+(void)changeConstraintView:(UIView *)view constraintHeight:(float)constraintHeight
{
    NSArray* constrains = view.constraints;
    for (NSLayoutConstraint* constraint in constrains) {
        if (constraint.firstAttribute == NSLayoutAttributeHeight) {
            constraint.constant = constraintHeight;
        }
    }
}

/**
 *  改变视图的约束
 *
 *  @param view           需要改变的视图
 *  @param constraintHeight 约束的尺寸
 */
+(void)changeConstraintView:(UIView *)view constraintWidth:(float)constraintWidth
{
    NSArray* constrains = view.constraints;
    for (NSLayoutConstraint* constraint in constrains) {
        if (constraint.firstAttribute == NSLayoutAttributeWidth) {
            constraint.constant = constraintWidth;
        }
    }
}

+(UIView *)createLineWithFrame:(CGRect)frame{
    UIView *line = [[UIView alloc ] initWithFrame:frame];
    line.backgroundColor = GLOBLE_GRAY_COLOR_1;
    return line;
}


+(NSMutableAttributedString *)getAttributeString:(NSArray *)attributeStringList{
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] init];
    
    for (NSInteger i = 0; i < attributeStringList.count; i ++) {
        MainInfoTitleFields *attributeStringModel = attributeStringList[i];
        
        CGFloat fontSize = 14;
        if (attributeStringModel.fontSize && [attributeStringModel.fontSize floatValue] != 0) {
            fontSize = [attributeStringModel.fontSize floatValue];
        }
        
       
        
        NSMutableAttributedString *attriString = [attributeStringModel.Text setAttributeWithColor:[CommoneTools getHexColor:attributeStringModel.Color]
                                                                                             font:[UIFont systemFontOfSize:fontSize] isLine:attributeStringModel.isLine];
        if (attriString) {
               [attr appendAttributedString:attriString];
        }
     
    }
    return attr;
}

// 获取地点后面附加的文字
+(NSAttributedString *)getBusLineStartLocationString:(NSString *)locationString extion:(NSString *)extion{
    NSMutableArray *mutableString = [[NSMutableArray alloc ] init];
    [mutableString addObject:[MainInfoTitleFields getInstaceWithText:locationString hexColor:GLOBLE_TEXT_COLOR_HEX fontSize:@"14"]];
    
    if (extion) {
        [mutableString addObject:[MainInfoTitleFields getInstaceWithText:extion hexColor:GLOBLE_ORANGE_COLOR_HEX fontSize:@"12"]];
    }
    
    return [CommoneTools getAttributeString:mutableString];
}

// 获取线路价格属性文字
+(NSMutableAttributedString *)getBusLinePriceString:(NSString *)ticketPrice isSale:(NSInteger)isSale salePrice:(NSString *)salePrice{
    NSMutableArray *mutableString = [[NSMutableArray alloc ] init];
    [mutableString addObject:[MainInfoTitleFields getInstaceWithText:@"¥" hexColor:GLOBLE_GRAY_COLOR_5_HEX fontSize:@"10"]];
    [mutableString addObject:[MainInfoTitleFields getInstaceWithText:[NSString stringWithFormat:@"%@  ",ticketPrice] hexColor:GLOBLE_ORANGE_COLOR_HEX fontSize:@"18"]];
    // 有折扣
    if (isSale == 1) {
        NSString *linePrice = [NSString stringWithFormat:@"%@元",salePrice];
        [mutableString addObject:[MainInfoTitleFields getInstaceWithText:linePrice hexColor:GLOBLE_GRAY_COLOR_5_HEX fontSize:@"10" isLine:YES]];
    }
    return [CommoneTools getAttributeString:mutableString];
}



@end
